package com.rdc.qingkou.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextPaint;
import android.util.AttributeSet;

import com.rdc.qingkou.R;

/**
 * Created by asus on 2017/8/6.
 */

public class PlayButton extends AppCompatTextView {
    private Paint mBigCirclePaint;
    private Paint mSmallCirclePaint;
    private Paint mSectorPaint;
    private TextPaint mTextPaint;
    private RectF mRect;
    private float mStripeWidth;
    private float mCenterTextSize;
    private float mCurPercent;
    private float mRadius;
    private int mWidth;
    private int mHeight;
    private float x;
    private float y;
    private float mEndAngle;
    private String mText;
    public PlayButton(Context context) {
        super(context);
        initPaint();
    }

    public PlayButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        setAttrs(context,attrs);

    }

    public PlayButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
        setAttrs(context,attrs);
    }


    public void setAttrs(Context context,AttributeSet attrs){
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PlayButton);
        mStripeWidth = array.getDimension(R.styleable.PlayButton_StripeWidth,20f);
        mCenterTextSize = array.getDimension(R.styleable.PlayButton_CenterTextSize,40f);
        mRadius = array.getDimension(R.styleable.PlayButton_Radius,166.5f);
    }
    private void initPaint() {

        mBigCirclePaint = new Paint();
        mBigCirclePaint.setAntiAlias(true);
        mBigCirclePaint.setStyle(Paint.Style.FILL);
        mBigCirclePaint.setColor(Color.parseColor("#FFBCD8B6"));

        mSmallCirclePaint = new Paint();
        mSmallCirclePaint.setColor(Color.WHITE);
        mSmallCirclePaint.setAntiAlias(true);


        mRect = new RectF();
        mSectorPaint = new Paint();
        mSectorPaint.setAntiAlias(true);
        mSectorPaint.setColor(Color.parseColor("#FF8AB077"));

        mTextPaint = new TextPaint();
        mSectorPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.parseColor("#FF8AB077"));

        mText = "点击试听";

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获取测量大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // match_parent
        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            mRadius = widthSize / 2;
            x = widthSize / 2;
            y = heightSize / 2;
            mWidth = widthSize;
            mHeight = heightSize;
        }


        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            mWidth = (int) (mRadius * 2);
            mHeight = (int) (mRadius * 2);
            x = mRadius;
            y = mRadius;
        }
        setMeasuredDimension(mWidth, mHeight);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mEndAngle = (int) (mCurPercent * 0.003);
        canvas.drawCircle(x, y, mRadius, mBigCirclePaint);

        mRect.right = mWidth;
        mRect.bottom = mHeight;
        canvas.drawArc(mRect, 270, mEndAngle, true, mSectorPaint);

        canvas.drawCircle(x, y, mRadius - mStripeWidth, mSmallCirclePaint);


        mTextPaint.setTextSize(mCenterTextSize);
        mTextPaint.setAntiAlias(true);
        float textLength = mTextPaint.measureText(mText);
        canvas.drawText(mText, x - textLength / 2, y+mCenterTextSize/2, mTextPaint);


    }

    public void setPercent(int percent) {
        mCurPercent = percent;
        invalidate();
    }

    public void setText(String text){
        mText = text;
        invalidate();
    }


}

